package com.qc31.monitorlib.ui.carmonitor

import autodispose2.autoDispose
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.comlib.UserConstant
import com.qc31.comlib.entity.GpsDetail
import com.qc31.comlib.entity.Legend
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.concurrent.TimeUnit

/**
 *@Author Jasper
 *@Time   2020/7/31
 *@Desc   地图
 */
class CarMapViewModel : AutoDisposeViewModel() {
    var locaEntity: GpsDetail? = null

    //是否处于显示状态
    var isHidden = false

    var isCarName: Boolean
        get() = CacheMMKV.decodeBoolean("car_isCarName")
        set(value) = CacheMMKV.encode("car_isCarName", value)

    var mMapType: Int
        get() = CacheMMKV.decodeInt("car_isMapNormal", AMap.MAP_TYPE_NORMAL)
        set(value) = CacheMMKV.encode("car_isMapNormal", value)

    var isCarTraffic: Boolean
        get() = CacheMMKV.decodeBoolean("car_isCarTraffic")
        set(value) = CacheMMKV.encode("car_isCarTraffic", value)

    val locationSub: PublishSubject<GpsDetail> = PublishSubject.create()
    val legendSub: PublishSubject<MutableList<Legend>> = PublishSubject.create()


    var carId: String? = null
        set(value) {
            if (value != null && !isHidden) {
                stopTime()
                startTime(value)
            }
            field = value
        }

    /**
     * 获取定位信息
     */
    private fun getLocation(carId: String?) {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
            stopTime()
        } else
            ServiceHelper.getRepository().getGpsRInfo("", carId)
                .autoDispose(this)
                .subscribe({
                    if (it.isNullOrEmpty()) {
                        toast(R.string.toast_no_location_msg)
                        stopTime()
                        locaEntity = null
                    } else {
                        locaEntity = it[0]
                        locationSub.onNext(locaEntity!!)
                    }
                }, error)
    }

    /**
     * 获取定位信息
     */
    private fun getLocation2(carId: String?) {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
            stopTime()
        } else
            ServiceHelper.getRepository().getGpsRInfo("", carId)
                .autoDispose(this)
                .subscribe({
                    if (it.isNullOrEmpty()) {
                        toast(R.string.toast_no_location_msg)
                        stopTime()
                        locaEntity = null
                    } else {
                        toast(R.string.toast_refresh_success)
                        locaEntity = it[0]
                        locationSub.onNext(locaEntity!!)
                    }
                }, error)
    }

    /**
     * 获取图例
     */
    fun getLegend(carId: String?) {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        toastLoading(true)
        ServiceHelper.getRepository().getLegend(carId).autoDispose(this).subscribe({
            if (it.isNullOrEmpty()) {
                toast(R.string.toast_car_no_legend)
            } else {
                if (UserConstant.current_ip == "113.105.152.93") {
                    val filter = it.filter { legend ->
                        legend.stateIndex != "6"
                    }.toMutableList()
                    legendSub.onNext(filter)
                } else {
                    legendSub.onNext(it)
                }
            }
            toastLoading(false)
        }, error)
    }

    private var subscribe: Disposable? = null

    /**
     * 开始请求
     */
    fun startTime(carId: String) {
        subscribe?.dispose()
        subscribe = Flowable.interval(0, 10, TimeUnit.SECONDS)
            .autoDispose(this)
            .subscribe {
                getLocation(carId)
            }
    }

    fun refresh() {
        getLocation(carId)
    }

    fun refresh2() {
        getLocation2(carId)
    }

    /**
     * 停止请求
     */
    fun stopTime() {
        subscribe?.dispose()
    }

    /**
     * 计算LatLngBounds
     */
    fun setBounds(vararg latLng: LatLng): LatLngBounds {
        val builder = LatLngBounds.builder()
        for (item in latLng) {
            builder.include(item)
        }
        return builder.build()
    }

}
